<template>
  <div>
    <!--页头-->
    <el-card class="box-card">
      <el-page-header content="商品审核"></el-page-header>
      <el-button type="primary" icon="el-icon-search" @click="onSubmit">条件筛选</el-button>
      <el-form :model="searchMap" label-width="120px">
        <el-form-item label="输入搜索：">
          <el-input v-model="searchMap.name" placeholder="商品名称/商品货号"></el-input>
        </el-form-item>
        <el-form-item label="商品分类：">
          <el-select v-model="searchMap.category1Id" placeholder="请选择商品分类">
            <el-option label="不选择" value></el-option>
            <el-option
              v-for="item in categories"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品品牌：">
          <el-select v-model="searchMap.brandId" placeholder="请选择品牌">
            <el-option label="不选择" value></el-option>
            <el-option v-for="item in brands" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="box-card">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="id" label="编号" width="140"></el-table-column>
        <el-table-column label="商品图片" width="180">
          <template slot-scope="scope">
            <img :src="scope.row.image" width="160px" height="80px" />
          </template>
        </el-table-column>
        <el-table-column prop="name" label="商品名称" width="180"></el-table-column>
        <el-table-column prop="sn" label="货号" width="180"></el-table-column>
        <el-table-column prop="isMarketable" label="标签" width="130">
          <template slot-scope="scope">
            <el-switch
              :value="scope.row.isMarketable"
              inactive-text="是否上架"
              disabled
              active-value="1"
              inactive-value="0"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="saleNum" label="销量" width="80"></el-table-column>
        <el-table-column prop="status" label="审核状态" width="130">
          <template slot-scope="scope">
            <el-switch
              :value="scope.row.status"
              inactive-text="是否审核"
              disabled
              active-value="1"
              inactive-value="0"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="* 操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              @click="deletedTableData(scope.$index, scope.row)"
            >查看</el-button>
            <el-button type="primary" size="mini" @click="openDialog(scope.$index, scope.row)">审核</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="pageSizeList"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
      <el-row :gutter="20" align="center">
        <el-col :span="6">
          <div class="grid-content bg-purple">
            <span>商品名称:</span>
          </div>
        </el-col>
        <el-col :span="15">
          <div class="grid-content bg-purple">
            <span>{{rows.name}}</span>
          </div>
        </el-col>
      </el-row>
      <el-form :model="rows" label-width="86px">
        <el-form-item label="商品审核：">
          <el-radio-group v-model="rows.status">
            <el-radio label="1">审核通过</el-radio>
            <el-radio label="2">审核不通过</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="反馈详情：">
          <el-input type="textarea" :v-model="rows.introduction"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateStatus()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      searchMap: {
        status: 0
      },
      tableData: [],
      categories: [],
      brands: [],
      currentPage: 1, // 页号
      pageSize: 10, // 页大小
      pageSizeList: [5, 10, 20, 30],
      total: 0,
      dialogVisible: false,
      rows: {} // 行数据
    }
  },
  methods: {
    updateStatus () {
      if (this.rows.status > 0) {
        if (this.rows.status === '2') {
            this.rows.isDelete = 1
        }
        console.log(this.rows)
        this.$http.post(`goods/spu/updateStatus`, this.rows)
        this.rows = {}
        this.dialogVisible = false
        alert("审核完毕")
        this.initDatas()
      } else {
        alert("请选择审核状态")
      }
        this.initDatas()
    },
    handleClose (done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          this.rows = {}
          this.dialogVisible = false
          done();
        })
        .catch(_ => { });
    },
    // 打开
    openDialog (index, rows) {
      this.dialogVisible = true
      this.rows = rows
    },
    async handleSizeChange (val) {
      this.pageSize = val;
      this.getSkuList();
    },
    async handleCurrentChange (val) {
      this.currentPage = val;
      this.initDatas();
    },
    onSubmit () {
      this.initDatas()
    },
    async initDatas () {
      const { data: res } = await this.$http.post(`goods/spu/initCommodityAudit/${this.currentPage}/${this.pageSize}`, this.searchMap)
      this.tableData = res.data.spuList
      this.brands = res.data.brands
      this.categories = res.data.categories
      this.total = res.data.total[0]
    }
  },
  async created () {
    this.initDatas()
  }
}
</script>

<style  scoped>
</style>
